package edu.neusoft.yuejian_09.model;

import java.util.List;

import edu.neusoft.yuejian_09.base.Constant;
import edu.neusoft.yuejian_09.base.RetrofitBaseModel;
import edu.neusoft.yuejian_09.base.RetrofitListener;
import edu.neusoft.yuejian_09.bean.Train;
import edu.neusoft.yuejian_09.bean.TrainOrder;
import edu.neusoft.yuejian_09.service.TrainService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static edu.neusoft.yuejian_09.base.Constant.BASE_URL;

/**
 * Created by 爽 on 2017/10/12.
 */

public class TrainModel extends RetrofitBaseModel {
    private int flag=1;
    public TrainService trainService;
    public TrainModel(){
        this.trainService=retrofit.create(TrainService.class);
    }

    /*获取训练列表*/
    public void astivitylist( final RetrofitListener listener){
        TrainService trainService=retrofit.create(TrainService.class);
        Call call=trainService.getAllTrains();
        Callback<List<Train>> callback=new Callback<List<Train>>(){
            @Override
            public void onResponse(Call<List<Train>> call, Response<List<Train>> response ){
                List<Train> data=response.body();
                listener.onSuccess(data,flag);//userId这里有问题
            }
            @Override
            public  void onFailure(Call<List<Train>> call,Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    /*获取菜鸟列表*/
    public void cainiaolist( final RetrofitListener listener){
        TrainService trainService=retrofit.create(TrainService.class);
        Call call=trainService.getCainiaoTrain();
        Callback<List<Train>> callback=new Callback<List<Train>>(){
            @Override
            public void onResponse(Call<List<Train>> call, Response<List<Train>> response ){
                List<Train> data=response.body();
                listener.onSuccess(data,flag);//userId这里有问题
            }
            @Override
            public  void onFailure(Call<List<Train>> call,Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    /*获取进阶列表*/
    public void jinjielist( final RetrofitListener listener){
        TrainService trainService=retrofit.create(TrainService.class);
        Call call=trainService.getJinjieTrain();
        Callback<List<Train>> callback=new Callback<List<Train>>(){
            @Override
            public void onResponse(Call<List<Train>> call, Response<List<Train>> response ){
                List<Train> data=response.body();
                listener.onSuccess(data,flag);//userId这里有问题
            }
            @Override
            public  void onFailure(Call<List<Train>> call,Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    /*获取入门列表*/
    public void rumenlist( final RetrofitListener listener){
        TrainService trainService=retrofit.create(TrainService.class);
        Call call=trainService.getRumenTrain();
        Callback<List<Train>> callback=new Callback<List<Train>>(){
            @Override
            public void onResponse(Call<List<Train>> call, Response<List<Train>> response ){
                List<Train> data=response.body();
                listener.onSuccess(data,flag);//userId这里有问题
            }
            @Override
            public  void onFailure(Call<List<Train>> call,Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    //获取培训详情
    public void getTrainById(int Train_id, final RetrofitListener listener){
        TrainService activityService=retrofit.create(TrainService.class);
        Call call=activityService.getTrainById(Train_id);
        Callback<Train> callback=new Callback<Train>(){
            @Override
            public void onResponse(Call<Train> call, Response<Train> response){

                listener.onSuccess(response.body(),flag);
            }
            @Override
            public void onFailure(Call<Train> call,Throwable t){
                t.printStackTrace();
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    //搜索课程
    public void getTrainBySearch(String SearchKey, final RetrofitListener listener){
        TrainService trainService=retrofit.create(TrainService.class);
        Call call=trainService.getTrainBySearch(SearchKey);
        Callback<List<Train>> callback=new Callback<List<Train>>(){
            @Override
            public void onResponse(Call<List<Train>> call, Response<List<Train>> response ){
                List<Train> data=response.body();
                listener.onSuccess(data,flag);//userId这里有问题
            }
            @Override
            public  void onFailure(Call<List<Train>> call, Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    //购买课程
    public void insertOrder(int user_id,int train_id, final RetrofitListener listener){
        Call call=trainService.insertOrder(user_id,train_id);
        bindCallback(call,listener, Constant.TRAIN);
    }

}
